$(function(){
	var superviseId={};
	var produceId={};
	var checkPortId=[];//选中模版的id
	var checkPort=[];//选中模版的名字
	var choiseInfoId=[];
	var checkPortName=[];

	requestPort();//请求分口数据
	
	//折叠菜单 
	$("#portModel").on("click",".myCollapsibleExample>a",function(){
				
		if($(this).parents(".form-group").find(".collapse").attr("class").indexOf("heiauto")>-1)
		{

			$(this).parents(".form-group").find(".collapse").removeClass("heiauto");
			$(this).parents(".form-group").find(".collapse").addClass("hei");
		}
		else
		{
			
			$(this).parents(".form-group").find(".collapse").removeClass("hei");
			$(this).parents(".form-group").find(".collapse").addClass("heiauto");
		}
		
	})
	//各个分口的查询单击
	$("#portModel").on("click",".BtnSearch",function(){
		var flagid=$(this).attr("data-flagId");
		var name=$(this).attr("data-name");
	
		requestSearch(flagid,name);
	})
	/*//确认提交
	$("#submi").click(function(){
		requestSubmit();
	})*/
	//添加分口模版
	function portModel(info)
	{
		

		for(var i=0;i<info.length;i++)
		{
			if(info[i].portName=="监管" || info[i].portName=="生产")
			{
				checkPortId.push(info[i].id);
    			checkPort.push(info[i].portName+"安全情况");
    			if(info[i].portName=="监管"){
    				checkPortName.push("jgInfo");
    			}else{
    				checkPortName.push("scInfo");
    			}
			}
			
		}

     	//循环添加分口模版
     	for(var i=0;i<checkPort.length;i++)
     	{
     		var html=' <div class="form-group" name="'+checkPort[i]+'" data-name='+checkPortName[i]+' id='+checkPortId[i]+'><label for="inputEmail3" class="col-xs-2 control-label">'+checkPort[i]+':</label><div class="form-group" style="margin-top:10px; "><div  class="resour sourceCheck" class="col-xs-5">'
			+'<!-- 后台请求数据填充 --></div><div class="col-xs-2"><input datepicker type="text" class="form-control datepicker" id="valStart'+checkPortId[i]+'" readonly="readonly" placeholder="请选择起始时间"></div><div class="col-xs-2">'
			+'<input datepicker type="text" class="form-control datepicker" id="valEnd'+checkPortId[i]+'" readonly="readonly" placeholder="请选择终止时间"></div><div class="col-xs-1"><button type="button" class="BtnSearch" class="btn btn-primary" data-name= '+checkPortName[i]+' data-flagId='+checkPortId[i]+'>查询</button></div></div><div class="myCollapsibleExample "><a class="btn btn-success btn-block" data-flag="'+checkPortId[i]+'" data-toggle="collapse" >'
			+'点击收缩</a></div> <div id="demo'+checkPortId[i]+'" class="collapse heiauto"> <h3>用户录入的'+checkPort[i]+'的信息</h3><table  class="table  table-striped"></table> </div> ';
			$("#portModel").append(html);
			
     	}
     	requestSource();//添加渠道
		datatime();//时间初始化
	}
	//请求渠道
	function requestSource()
	{
	
		var url="getAllInfomationSource.do";//渠道地址
		var data={};
		var object={};
	
		YuXi.util.get(url,"null",function(data)
		{
			object=data.object;
			for(var i=0;i<object.length;i++)
			{
				var html= "<label class='checkbox-inline'><input type='checkbox'  class='check' data-na="+object[i].name+" value="+object[i].id+" name='source'><span>"+object[i].name+"</span></label>";
				$(".sourceCheck").append(html);
				
			}
		
		},function(){});//请求数据
	}
	//向后台请求安全分口
	function requestPort()
	{
		var url="getAllInformationPort.do";//请求安全分口信息
		var object={};

	
		YuXi.util.get(url,"null",function(data)
		{
			object=data.object;
			for(var i=0;i<object.length;i++)
			{
				var html= "<label class='checkbox-inline'><input type='checkbox'  class='check' data-na="+object[i].portName+" value="+object[i].id+" name='port'><span>"+object[i].portName+"</span></label>";
				$("#safePort").append(html);
			}
			portModel(object);

		},function(){

		})

	}
	//根据条件请求查询数据
	function requestSearch(flagId,infonames)
	{	
		var url="policeQuery.do";//地址
		var data={};//条件
		var check=[];//选中的渠道
		$("#"+flagId).find("input[name='source']:checked").each(function () {
    		check.push($(this).val());
     	});
		
		var start=$("#valStart"+flagId).val();
		var end=$("#valEnd"+flagId).val();
		var array=[];
		data.informationSource=check.join(",");
		data.salfPort=flagId;//分口Id
		array=YuXi.timestamp.get(start,end,null);
		data.startTimestamp=array[0];//起始时间
		data.endTimestamp=array[0];//结束时间
		var infomation={};//$ajax返回的值
		//向后台请求数据
		YuXi.util.get(url,data,function(data){
			infomation=data.object;
			var infoid=[];
		    
			$("#demo"+flagId).find("table").html("");
		    $("#demo"+flagId).find("table").append(' <thead><th>信息编号</th><th>信息内容</th><th>录入参与人</th><th>标准</th><th>措施</th><th>处置时限</th><th>处置情况</th><th>处置时间</th><th>处置人</th></thead><tbody >');
			for(var i=0;i<infomation.length;i++)
			{
				infoid.push(infomation[i].informationAccount);//将信息ID单独抓出来
				var zeRenTime=infomation[i].zeRenTime==undefined?"":infomation[i].zeRenTime;
				var taskCompletion=infomation[i].taskCompletion==undefined?"":infomation[i].taskCompletion;
				var listt=JSON.parse(taskCompletion);
				taskCompletion="";
				var taskDispatchTime="";
				for(var k=0;k<listt.length;k++){
					taskCompletion+=listt[k].name+":"+listt[k].text;	
					taskDispatchTime+=listt[k].name+":"+listt[k].time;	
				}				
				var zeRenPerSonNames=infomation[i].zeRenPerSonNames==undefined?"":infomation[i].zeRenPerSonNames;
				var html='<tr> <td name='+infonames+' value='+infomation[i].id+'>'+infomation[i].informationAccount+'</td><td>'+infomation[i].informationConternt+'<input type="text" name='+infonames+' value='+infomation[i].id+' style="display:none"></input></td><td>'+infomation[i].inputPerSonNames+'</td><td>'+infomation[i].standard+'</td><td>'+infomation[i].mesure+'</td>'
				+'<td>'+zeRenTime+'</td><td>'+taskCompletion+'</td><td>'+taskDispatchTime+'</td><td>'+zeRenPerSonNames+'</td></tr>'
				$("#demo"+flagId).find("table").append(html);
			}
			 $("#demo"+flagId).find("table").append("</tbody>");
			 var name="";
			 for(var i=0;i<checkPortId.length;i++)
			 {
			 	if(checkPortId[i]==flagId)
			 	{
			 		name=checkPort[i];
			 	}
			 }
			choiseInfoId.push({
				safetyPortId:flagId,//分口id
				portNames:name,
				informationAccount:infoid//在此分口查询的信息
			});
			
			
		},function(){
		})

	}
	//提交所有信息
	function requestSubmit()
	{
		var data={};
		var url="";//地址
		data.owntotal=$("#owntotal").val();//总体犯情
		data.ownquestion=$("#ownquestion").val();//需要解决的情况
		data.ownsuggest=$("#ownsuggest").val();//对工作的建议
		data.owngood=$("#owngood").val();//好的做法
		data.infomation=choiseInfoId;//所有查询过的表
		

		alert(JSON.stringify(data));
		YuXi.util.get(url,data,function(data){
			myTool.alert({		
			title:"提示",
			context:"提交成功",
			isMask:true,
			callback:function(){
				}
			});
		},function(){
			myTool.alert({		
			title:"提示",
			context:"提交失败",
			isMask:true,
			callback:function(){
				}
			});
		})
	}

//日历插件初始化
function datatime()
{
	$( ".datepicker" ).datepicker({
	      showOn: "button",  
	      buttonImage: "/yuxi/images/calendar.gif", 
	      buttonImageOnly: true,  
	      changeMonth: true,
	      changeYear: true,
	      dateFormat: 'yy-mm-dd',
	      beforeShow: function (input, inst) {  
               $.datepicker._pos = $.datepicker._findPos(input);   
               $.datepicker._pos[1] += input.offsetHeight + document.body.scrollTop; // 加入body的scrollTop修正jquery在ie7中定位不準的問題  
               inst.dpDiv.css('font-size','70%');   
           }  
	    });
	    jQuery(function($){  
	                $.datepicker.regional['zh-CN'] = {  
	                    closeText: '关闭',  
	                    prevText: '<上月',  
	                    nextText: '下月>',  
	                    currentText: '今天',  
	                    monthNames: ['一月','二月','三月','四月','五月','六月',  
	                    '七月','八月','九月','十月','十一月','十二月'],  
	                    monthNamesShort: ['一','二','三','四','五','六',  
	                    '七','八','九','十','十一','十二'],  
	                    dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  
	                    dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],  
	                    dayNamesMin: ['日','一','二','三','四','五','六'],  
	                    weekHeader: '周',  
	                    dateFormat: 'yy-mm-dd',  
	                    firstDay: 1,  
	                    isRTL: false,  
	                    showMonthAfterYear: true,  
	                    yearSuffix: '年'};  
	                $.datepicker.setDefaults($.datepicker.regional['zh-CN']);  
	  			});

	    $(".ui-datepicker-trigger").click(function(){
	    	var scoh=$(document).scrollTop();
	    	var top=$("#ui-datepicker-div").css("top");
	    	$("#ui-datepicker-div").css("top",top-scoh+"px");
	    })
}


})




